package com.hundsun.yr.universal.p2p;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.ansen.http.entity.HttpConfig;
import com.ansen.http.net.HTTPCaller;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseApplication;
import com.hundsun.yr.universal.library.utils.ImageLoaderHelper;
import com.hundsun.yr.universal.p2p.bean.ProtocolBean;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.VolleyHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.qiyukf.unicorn.api.SavePowerConfig;
import com.qiyukf.unicorn.api.StatusBarNotificationConfig;
import com.qiyukf.unicorn.api.Unicorn;
import com.qiyukf.unicorn.api.YSFOptions;
import com.qiyukf.unicorn.apicloud.QYResUtils;
import com.qiyukf.unicorn.utils.DemoCache;
import com.qiyukf.unicorn.utils.DemoPreferences;
import com.qiyukf.unicorn.utils.Utils;
import com.qiyukf.unicorn.utils.update.UtilsApp;
import com.squareup.picasso.Picasso;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;
import java.util.Map;


import cn.jpush.android.api.JPushInterface;


/**
 * @Title: HsApplication.java 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @date 2015-10-14 下午6:09:16 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class HsApplication extends BaseApplication {
	/**
	 *  用于获取按钮验证码存放倒计时时间
	 */
	public static Map<String, Long> timeMap;
	
	/**
	 * 记录当前已经进入后台
	 */
	public static boolean goGesture = true;
	public static boolean goFinger = true;
	public static boolean isSrcreenOn ;
	/**是否上传地址*/
	public static boolean isLocation = false;
	public static ArrayList<ProtocolBean> protocolNameList = new  ArrayList<ProtocolBean>();

    @Override
    public void onCreate() {
        super.onCreate();


		HttpConfig httpConfig = new HttpConfig();
		httpConfig.setAgent(true);//有代理的情况能不能访问
		httpConfig.setDebug(true);//是否debug模式 如果是debug模式打印log
		httpConfig.setTagName("ansen");//打印log的tagname

		//可以添加一些公共字段 每个接口都会带上
		httpConfig.addCommonField("pf", "android");
		httpConfig.addCommonField("version_code", "" + UtilsApp.getVersionCode(getApplicationContext()));

		//初始化HTTPCaller类
		HTTPCaller.getInstance().setHttpConfig(httpConfig);
        // TODO: 2017/5/22 调试阶段关闭，上线之后开启
/*        CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());*/
//		LeakCanary.install(this);

		VolleyHelper.getInstance().init(getBaseContext());
        ImageLoader.getInstance().init(ImageLoaderHelper.getInstance(this).
        		getImageLoaderConfiguration(Config.ImagePaths.IMAGE_LOADER_CACHE_PATH));
        DemoPreferences.init(this);

        QYResUtils.init(this);

        //you can use "new FrescoImageLoader()" or "new PicassoImageLoader()"
        if (!Unicorn.init(this, ysfAppId(), options(), new UILImageLoader())) {
            Log.w("demo", "init qiyu sdk error!");
        }

        if (inMainProcess(this)) {
            DemoCache.context = getApplicationContext();
            ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this));
            Fresco.initialize(this);
            Picasso.with(this);
        }


     // 新浪微博
        PlatformConfig.setSinaWeibo(getString(R.string.sina_app_key),getString(R.string.sina_app_secret),"");
        PlatformConfig.setWeixin("wxf8d7745e93e50da0", "6862464c297da74b94fb29674a438c5d");
        PlatformConfig.setQQZone(getString(R.string.qq_app_id), getString(R.string.qq_app_key));
        UMShareAPI.get(this);
		//集成友盟统计
		MobclickAgent. startWithConfigure(new MobclickAgent.UMAnalyticsConfig(this,getString(R.string.umen_app_key),getString(R.string.channel)));
		MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType. E_UM_NORMAL);

		//极光推送
		JPushInterface.setDebugMode(true);

		JPushInterface.init(this);

		CommTool.isApplicationBroughtToBackground(this);
    }

	private String ysfAppId() {
    	return  DemoPreferences.getYsfAppKey();
    }
    

	private YSFOptions options() {
		YSFOptions options = new YSFOptions();
		options.statusBarNotificationConfig = new StatusBarNotificationConfig();
		options.statusBarNotificationConfig.notificationSmallIconId = R.drawable.ic_status_bar_notifier;
//		options.statusBarNotificationConfig.notificationEntrance = null;
		options.savePowerConfig = new SavePowerConfig();
		return options;
	}

	public static boolean inMainProcess(Context context) {
		String packageName = context.getPackageName();
		String processName = Utils.getProcessName(context);
		return packageName.equals(processName);
	}
	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
		MultiDex.install(this) ;
	}
}
